
#include <etcd/Client.hpp>
#include <etcd/Watcher.hpp>
#include <etcd/Value.hpp>
void WatchListen(etcd::Response res)
{
    for(auto e:res.events())
    {
        if(e.event_type()==etcd::Event::EventType::PUT)
        {
            std::cout<<"键值发生修改"<<std::endl;
            std::cout<<"before: "<<e.prev_kv().key()<<":"<<e.prev_kv().as_string()<<std::endl;
            std::cout<<"now: "<<e.kv().key()<<":"<<e.kv().as_string()<<std::endl;
        }
        else if(e.event_type()==etcd::Event::EventType::DELETE_)
        {
            std::cout<<"数据发生删除"<<std::endl;
            std::cout<<"now: "<<e.kv().key()<<":"<<e.kv().as_string()<<std::endl;
        }
    }
}

int main()
{
    etcd::Client etcd("http://127.0.0.1:2379");
    etcd::Response res = etcd.ls("/test").get();

    for(auto e:res.events())
    {
        std::cout<<"当前值"<<e.kv().key()<<e.kv().as_string()<<std::endl;
    }

    etcd::Watcher watcher(etcd,"/test",WatchListen,true);
    watcher.Wait();
    return 0;
}